module view {
    export class PlayerInfoView extends eui.Component {
        closeBtn: eui.Button
        maxHpLabel: eui.Label
        attackLabel: eui.Label
        defendLabel:eui.Label
        critLabel:eui.Label
        critHurtLabel: eui.Label
        energyLabel: eui.Label
        dodgeLabel: eui.Label
        luckyLabel: eui.Label
        moneyLabel: eui.Label
        lvlLabel: eui.Label
        expLabel: eui.Label
        battlePetLabel: eui.Label
        battlePetsGroup: eui.Group
        petCons: any[]
        public constructor() {
            super()
            this.addEventListener(eui.UIEvent.COMPLETE,this.onComplete,this)
            this.skinName = "resource/skin/PlayerInfoSkin.exml"
            
        }

        protected onComplete(): void {
            this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.close,this)
            this.showPlayerInfo()
            this.showPets()
        }

        protected close(): void {
            this.parent.removeChild(this)
        }
        
        protected showPlayerInfo(): void{
            this.attackLabel.text = gplayer.fightAttri.attack + ''
            this.defendLabel.text = gplayer.fightAttri.defend + ''
            this.critLabel.text = gplayer.fightAttri.crit + ''
            this.critHurtLabel.text = gplayer.fightAttri.critHurt + ''
            this.dodgeLabel.text = gplayer.fightAttri.dodge + ''
            this.luckyLabel.text = gplayer.fightAttri.lucky + ''
            this.moneyLabel.text = gplayer.basicInfo.money + ''
            this.lvlLabel.text = gplayer.basicInfo.lvl + ''
            this.expLabel.text = gplayer.basicInfo.exp + ''
            this.energyLabel.text = gplayer.basicInfo.energy + ''
            this.maxHpLabel.text = gplayer.fightAttri.maxHp + ''        
        }
        
        private showPets():void{
            this.battlePetsGroup.removeChildren()
            this.petCons = []
            for(var i in gplayer.battlePets) {
                var pet = gplayer.battlePets[i]
                var selectButton: eui.RadioButton = new eui.RadioButton();
                selectButton.skinName = 'resource/control/PetRadioBtnSkin.exml'
                selectButton.label = templateName[pet.id]
                selectButton.groupName = "petsGroup"
                selectButton.horizontalCenter = 0
                this.battlePetsGroup.addChild(selectButton)
                selectButton.y += i * 70
                selectButton['originalY'] = i * 70
                selectButton.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.petBEGIN,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.petMove,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_END,this.petEND,this)
                selectButton.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE,this.petEND,this)
                this.petCons[i] = selectButton
                var petInfo = {
                    index: i,
                    flag: 1
                }
                selectButton.value = petInfo
            }
        }
        
        private petBEGIN(evt: egret.TouchEvent): void {
            var target = evt.target
            target['flag'] = true
            target['startPos'] = {
                x: evt.localX,
                y: evt.localY
            }
            evt.stopPropagation()
            target.parent.swapChildrenWithEnd(target)
        }
        private petMove(evt: egret.TouchEvent): void {
            var target = evt.target
            if(target['flag'] !== true) {
                return
            }
            var startPos = target['startPos']
            var relativeX = evt.localX - startPos.x
            var relativeY = evt.localY - startPos.y
            target.y += relativeY
        }

        private petEND(evt: egret.TouchEvent): void {
            var target = evt.target
            target['flag'] = false
            var y = target.y
            for(var i in this.petCons) {
                if(y < this.petCons[i]['originalY'] + 25 && y > this.petCons[i]['originalY'] - 25 && this.petCons[i] !== target) {
                    var j = i
                    var k = target.value.index
                    game.playerHandle.petTransPos(gplayer,j,k)
                    this.showPets()
                    return
                }
            }
            target.y = target['originalY']
        }
    }
}
